package club.hicode.daydayup.aop.frame;

import lombok.AllArgsConstructor;
import org.aopalliance.intercept.MethodInterceptor;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 在此处将原始信息增强，原始信息增强是通过 MethodInterceptor来实现的，避免了代理在此处混乱<br>
 *
 * @author Liu Chunfu
 * @date 2018-07-24 下午3:51
 **/
@AllArgsConstructor
public class JdkAopProxy implements InvocationHandler, AopProxy {

    private AdvisedSupport support;

    /**
     * 明确如何获得代理对象
     *
     * @return
     */
    @Override
    public Object getProxy() {
        return Proxy.newProxyInstance(getClass().getClassLoader(),
                new Class[]{support.getTargetSource().getTargetInterface()}, this);
    }

    /**
     * invoactionHanlder 其中3个参数 proxy,method,args
     *
     * @param proxy
     * @param method
     * @param args
     * @return
     * @throws Throwable
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        MethodInterceptor interceptor = support.getInterceptor();
        return interceptor.invoke(new CurMethodInvocation(support.getTargetSource().getTargetObj(), method, args));
    }
}
